<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;

class IndexController extends BaseController {
    public function index() {
		$code=$_SERVER["QUERY_STRING"];
		$code=str_replace('code=','',$code); 

		 
		if(!empty($code)){
			$this->redirect('callback', array('code'=>$code,'type'=>'qq'));
		}
		
		
		$category=M('category');
		$news=M('news');
		$images=M('images');
		//首页导航
		$categorys=$category->field('cid,name')->where(array('pid'=>1))->select();
		

		//开始推荐
		$click=$news->field('id,title,published,mid')->where(array('status'=>1))->order("click desc")->limit(4)->select();
			foreach($click as $k=>$v)
			{
				$mid=$v['mid'];
				$avatar=M('member')->where(array('uid'=>$mid))->getField('avatar');
				$click [$k]['avatar']=$avatar;
			}
		
		//散文
		$sanwen=$news->field('id,title')->where(array('cid'=>2,'status'=>1))->order("id desc")->limit(30)->select();
		$sanwen1=$news->field('id,title,image_id')->where(array('cid'=>2,'status'=>1,'is_recommend'=>1))->order("id desc")->limit(2)->select();
			foreach($sanwen1 as $k=>$v)
			{
				$image=$v['image_id'];
				$savepath=$images->field('savepath')->where(array('id'=>$image))->find();
				$sanwen1 [$k]['images']=$savepath;
			}
		//诗歌
		$shige=$news->field('id,title')->where(array('cid'=>3,'status'=>1))->order("id desc")->limit(30)->select();
		$shige1=$news->field('id,title,image_id')->where(array('cid'=>3,'status'=>1,'is_recommend'=>1))->order("id desc")->limit(2)->select();
			foreach($shige1 as $k=>$v)
			{
				$image=$v['image_id'];
				$savepath=$images->field('savepath')->where(array('id'=>$image))->find();
				$shige1 [$k]['images']=$savepath;
			}
		
		//杂文(后面几个栏目)
		$map['cid']= array('in','4,5,58');
		$map['status']=1;
		$zawen=$news->field('id,title')->where($map)->order("id desc")->limit(30)->select();
			$map1['cid']= array('in','4,5,58');
			$map1['status']=1;
			$map1['is_recommend']=1;
		$zawen1=$news->field('id,title,image_id')->where($map1)->order("id desc")->limit(2)->select();
			foreach($zawen1 as $k=>$v)
			{
				$image=$v['image_id'];
				$savepath=$images->field('savepath')->where(array('id'=>$image))->find();
				$zawen1 [$k]['images']=$savepath;
			}
		
		
		//网友推荐的诗歌
		$this->recommend=$news->field('id,title')->where(array('cid'=>3,'status'=>1,'is_recommend'=>1))->order("id desc")->limit(20)->select();
		
		
		//访问量榜
		$this->access=$news->field('id,title')->where(array('status'=>1))->order("access desc")->limit(18)->select();
		
		
		//最新发布的文章
		$this->news=$news->field('id,title')->where(array('status'=>1))->order("id desc")->limit(18)->select();
		
		//点赞最高的文章
		$this->like=$news->field('id,title')->where(array('status'=>1,'is_recommend'=>1))->order("access desc")->limit(18)->select();
		//友情链接
		$this->link=M('link')->where(array('display'=>1))->order("sort desc")->select();
		
		
		$this->assign("click", $click);
		$this->assign("sanwen", $sanwen);
		$this->assign("sanwen1", $sanwen1);
		$this->assign("shige", $shige);
		$this->assign("shige1", $shige1);
		$this->assign("zawen", $zawen);
		$this->assign("zawen1", $zawen1);
		$this->assign("categorys", $categorys);
        $this->display();
    }
	
	public function order() {

        $this->display();
    }

    /**
     * 首页导航列表页
     */
    function nav_list(){
		$category=M('category');
		$news=M('news');
		//首页导航
		$this->categorys=$category->field('cid,name')->where(array('pid'=>1))->select();
		
		//编辑推荐
		$this->recommend=$news->field('id,title')->where(array('status'=>1,'is_recommend'=>1))->order("id desc")->limit(10)->select();
		
		//访问量榜
		$this->access=$news->field('id,title')->where(array('status'=>1))->order("access desc")->limit(10)->select();
		
		//最新发布的文章
		$this->new=$news->field('id,title')->where(array('status'=>1))->order("id desc")->limit(10)->select();
		
		$cid=intval(I('get.cid'));
		if($cid<=o){
			$this->redirect("Base/empty");
		}
		$this->title=$category->where(array('cid'=>$cid))->getField('name');
		
		$where['cid']=$cid;
		$where['status']=1;
		$count =$news->where($where)->count();
		//进行第三方分页类配置
    		$page = array(
    				'total' => $count,//总数
    				'url' => !empty($param['url']) ? $param['url'] : '',//URL配置
    				'max' => !empty($param['max']) ? $param['max'] :10,//每页显示多少条记录
    				'url_model' => 1,//URL模式
    				'ajax' =>  !empty($param['ajax']) ? true : false,//开启ajax分页
    				'out' =>  !empty($param['out']) ? $param['out'] : false,//输出设置
    				'url_suffix' => true,//url后缀
    				'tags' => array('首页','上一页','下一页','尾页'),
    		);
		$p = new \Org\Util\Page($page);
		$list=$news->field('id,title,published,content')->where($where)->limit($p->pagerows(),$p->maxrows())->order("id desc")->select();

		
		$this->assign("list", $list); 
		$this->assign('pageNav',$p->get_page()); //输出页码
        $this->display();
	}
	
	/**
     * 文章内容页
     */
	 function article(){
		$category=M('category');
		$news=M('news');
		$member=M(member);
		$comment=M('comment');
		//首页导航
		$this->categorys=$category->field('cid,name')->where(array('pid'=>1))->select();
		
		//编辑推荐
		$this->recommend=$news->field('id,title')->where(array('status'=>1,'is_recommend'=>1))->order("id desc")->limit(10)->select();
		
		//访问量榜
		$this->access=$news->field('id,title')->where(array('status'=>1))->order("access desc")->limit(10)->select();
		
		//最新发布的文章
		$this->new=$news->field('id,title')->where(array('status'=>1))->order("id desc")->limit(10)->select();
		
		
		$id=intval(I('get.id'));
		if($id<=o){
			$this->redirect("Base/empty");
		}
		
		//面包屑
		$cid=$news->where(array('id'=>$id))->getField('cid');
		$cid_name=$category->where(array('cid'=>$cid))->getField('name');
		
		$access = cookie('access');
		if(empty($access)){
			$access_num=$news->where(array('id'=>$id))->getField('access');
			$data['access']=$access_num+1;
			$news->where(array('id'=>$id))->save($data);
		}
		$list=$news->field('id,title,published,content,mid,aid,like')->where(array('id'=>$id))->find();
			$mid=$list['mid'];
			$penname=$member->where(array('uid'=>$mid))->getField('penname');
			$list['penname']=$penname;
			
		if(empty($list)){
			$this->redirect("Base/empty");
		}
		
		//评论显示
		//$map['status']=1;
		$map['pid']=$id;
		$count =$comment->where($map)->count();
		//进行第三方分页类配置
    		$page = array(
    				'total' => $count,//总数
    				'url' => !empty($param['url']) ? $param['url'] : '',//URL配置
    				'max' => !empty($param['max']) ? $param['max'] :10,//每页显示多少条记录
    				'url_model' => 1,//URL模式
    				'ajax' =>  !empty($param['ajax']) ? true : false,//开启ajax分页
    				'out' =>  !empty($param['out']) ? $param['out'] : false,//输出设置
    				'url_suffix' => true,//url后缀
    				'tags' => array('首页','上一页','下一页','尾页'),
    		);
		$p = new \Org\Util\Page($page);
		$comment=$comment->where($map)->limit($p->pagerows(),$p->maxrows())->order("id desc")->select();
			foreach($comment as $k=>$v)
			{
				$author_id=$v['author_id'];
				$critic_id=$v['critic_id'];
				$avatar=$member->where(array('uid'=>$critic_id))->getField('avatar');
				$nickname=$member->where(array('uid'=>$author_id))->getField('nickname');
				$comment [$k]['nickname']=$nickname;
				$comment [$k]['avatar']=$avatar;
			}
		//生成cookie
		cookie('access',$id,86400);
		
		
		//作者的数据
		$mid=$news->where(array('id'=>$id))->getField('mid');
		$author=$member->field('uid,penname,intr,avatar')->where(array('uid'=>$mid))->find();
		
		//关注
		$tow=$author['uid'];
		$user=session('member');
		$one=$user['uid'];
		$follow=M('follow')->where(array('one'=>$one,'tow'=>$tow,'status'=>1))->find();
		
		$this->assign("cid", $cid);
		$this->assign("cid_name", $cid_name);
		$this->assign("follow", $follow);
		$this->assign("author", $author);
		$this->assign("comment", $comment); 
		$this->assign('pageNav',$p->get_page()); //输出页码
		$this->assign("list", $list); 
		$this->display(); 
	 }
	 
	 
	 //文章分享
	 public function share(){
		 if(IS_POST)
		 {
			 $pid=I('post.pid');
			 $share=I('post.share');
			 $sharenum=empty($share)?0:$share;
			 $data['id']=$pid;
			 $data['sharenum']=$sharenum;
			 dump($data);
			 $b=M('news')->where(array('id'=>$data['id']))->save($data);
		 }
		 else
		 {
			 $this->error("非法访问");
		 }
	 }
	 
	 //关注
	 public function follow(){
		 if(IS_POST)
		 {
			$data['one']=I('post.session');
			$data['tow']=I('post.uid');
			$data['addtime']=time();
			$data['status']=1;
			
			$b=M('follow')->where(array('one'=>$data['one'],'tow'=>$data['tow']))->find();
			if($b){
				$data1['status']=1;
				$data1['addtime']=time();
				$c=M('follow')->where(array('one'=>$data['one'],'tow'=>$data['tow']))->save($data1);
				if($c){
					$msg=array('info'=>'客官,这货关注成功了!!!','status'=>1);
				}else{
					$msg=array('info'=>'客官,这货不让你关注!!!','status'=>0);
				}
				
			}else{
				$a=M('follow')->add($data);
				if($a){
					$msg=array('info'=>'客官,这货关注成功了!!!','status'=>1);
				}else{
					$msg=array('info'=>'客官,这货不让你关注!!!','status'=>0);
				}
			}
			$this->ajaxReturn($msg);
		 }
		 else
		 {
			$this->error("非法访问"); 
		 }
		 
	 }

	 
	 //取消关注
	 public function delfollow(){
		 if(IS_POST)
		 {
			$data['one']=I('post.session');
			$data['tow']=I('post.uid');
			if($data['one']==$data['tow']){
				$msg=array('info'=>'客官，这家伙是你自己啊!!!','status'=>0);
			}else{
				$data1['status']=0;
				$data1['addtime']=time();
				$b=M('follow')->where(array('one'=>$data['one'],'tow'=>$data['tow']))->save($data1);
				if($b){
					$msg=array('info'=>'客官，这家伙已经离开你了!!!','status'=>1);
				}else{
					$msg=array('info'=>'客官，这家伙舍不得你啊!!!','status'=>0);
				}
			}
			$this->ajaxReturn($msg);
			
		 }
		 else
		 {
			 $this->error("非法访问"); 
		 }
		 
	 }
	 
	 //访问
	 public function visit(){
		if(IS_POST)
		{
			$data['one']=I('post.uid');
			$data['tow']=I('post.session');
			$data['addtime']=time();
			$data['status']=1;
			$b=M('visit')->add($data);
			if($b){
				$msg=array('info'=>'客官，这家伙等你很久了!!!','status'=>1);
			}else{
				$msg=array('info'=>'客官，这家伙不让你访问啊!!!','status'=>0);
			}
			$this->ajaxReturn($msg);
		}
		else{
			$this->error("非法访问");
		}
	 }
	 
	 //评论处理
	 public function comment(){
		if(IS_POST)
		{
			$data1=I('post.');
			$is_level=$data1['is_level'];
			$member=session('member');
			$data1['uid']=$member['uid'];//评论人id*/
			$author_id=$data1['author_id'];
			if($author_id==$data1['uid']){
				//说明是自己评论自己的
				$msg=array('info'=>'自己不能评论自己','status'=>0);
				
			}
			else
			{
				$data['critic']=$member['nickname'];
				$data['content']=$data1['content'];
				$data['status']=0;
				$data['addtime']=time();
				$data['pid']=$data1['pid'];
				$data['title']=M('news')->where(array('id'=>$data['pid']))->getField('title');
				$data['author_id']=$data1['author_id'];
				$data['critic_id']=$data1['uid'];
				$data['is_level']=$data1['is_level'];
				$b=M('comment')->add($data);
				if($b)
				{
					$msg=array('info'=>'评论成功，正在审核中....','status'=>1);
				}
				else
				{
					$msg=array('info'=>'评论失败','status'=>0);
				}
			}
			$this->ajaxReturn($msg);
			
		}
		else
		{
			$this->error("非法访问");
		}

		
	 } 
	 
	 //用户注册处理
	 public function reg(){
		if(IS_POST)
		{
			$member=M('member');
			$name=I('username');
			$password=I('password');
			$res=$member->where(array('nickname'=>$name))->find();
			if($res)
			{
				$msg=array('info'=>'客官，,这个垃圾账号被别人霸占了！！！','status'=>0);
			}
			else
			{
				$data['nickname']=$name;
				$data['pwd']=encrypt($password);
				$data['reg_date']=time();
				$data['login_time']=time();
				$b=$member->add($data);
				if($b)
				{
					$db=$member->where(array('uid'=>$b))->find();
					session('member',$db);
					$msg=array('info'=>'客官，,本宝宝等你很久了！！！','status'=>1);
				}
				else
				{
					$msg=array('info'=>'客官，,遇到了不明飞行物，失败了！！！','status'=>0);
				}
			}
			$this->ajaxReturn($msg);
		}
		else
		{
			$this->error("非法访问");
		}
		 
	 }
	 
	 //用户登录处理
	 public function login(){
		 if(IS_POST)
		 {
			 $member=M('member');
			 $name=I('username');
			 $password=I('password');
			 if(empty($name)){
				$msg=array('info'=>'客官，,这货账号为空了！！！','status'=>0);
				$this->ajaxReturn($msg);die;
			 }
			 if(empty($password)){
				$msg=array('info'=>'客官，,这货密码为空了！！！','status'=>0);
				$this->ajaxReturn($msg);die;
			 }
			 $where['nickname']=$name;
			 $row=$member->where($where)->find();
			 if(empty($row)){
				 $msg=array('info'=>'客官,这货把账号写错咯!!!','status'=>0);
				 $this->ajaxReturn($msg);die;
    		 }
			 if($row['pwd']!=encrypt($password)){
				$msg=array('info'=>'客官,这货把密码写错咯!!!','status'=>0);
				$this->ajaxReturn($msg);die;
			}
			$data['login_time']=time();
           	$data['login_ip'] = get_client_ip(0,true);
			$b=$member->where($where)->save($data);
			session('member',$row);
			$msg=array('info'=>'客官,本宝宝等你很久了!!!','status'=>1);
			$this->ajaxReturn($msg);
			 
		 }
		 else
		 {
			 $this->error("非法访问");
		 }
	 }
	 
	 //qq登录处理
	 public function qq_login($type = null){
		empty($type) && $this->error('参数错误');
		$sns = \Org\ThinkSDK\ThinkOauth::getInstance($type);
		//redirect($sns->gettest());
		redirect($sns->getRequestCodeURL());
	 }
	 
	  //授权回调地址
	public function callback($type = null, $code = null){
		(empty($type) || empty($code)) && $this->error('参数错误');
		$sns = \Org\ThinkSDK\ThinkOauth::getInstance($type);
		$extend = null;
		/*if($type == 'tencent'){
			$extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey'));
		}*/

		//请妥善保管这里获取到的Token信息，方便以后API调用
		//调用方法，实例化SDK对象的时候直接作为构造函数的第二个参数传入
		//如： $qq = ThinkOauth::getInstance('qq', $token);
		
		$token = $sns->getAccessToken($code , $extend);
		//获取当前登录用户信息
		if(is_array($token)){
			//$user_info = A('Type', 'Event')->$type($token);
			$user_info=$this->qq($token);
			$openid=$token['openid'];
			if(empty($openid)){
				$this->error("登录失败");
			}
			//dump($token);
			//dump($user_info);
			$member=M('member');
			$b=$member->where(array('openid'=>$openid))->find();
			if($b){
				//以前登录过
				session('member',$b);
              	
              	$data_login['login_time'] = time();
              	$data_login['login_ip'] = get_client_ip(0,true);
              	$member->where(array('openid'=>$openid))->save($data_login);
				//$this->redirect("Index/index");
				header("Location: http://www.okshiwen.cn/");
			}else{
				//第一次登录
				$data['nickname']=$user_info['name'];
				$data['penname']=$user_info['name'];
				$data['avatar']=$user_info['head'];
				$data['openid']=$openid;
              	$data['login_time']=time();
              	$data['login_ip'] = get_client_ip(0,true);
				$res=$member->add($data);
				$a=$member->where(array('uid'=>$res))->find();
				session('member',$a);
				header("Location: http://www.okshiwen.cn/");
			}
			
			
			
			
		}
	}
	
	public function qq($token){
		$qq = \Org\ThinkSDK\ThinkOauth::getInstance('qq', $token);
		$data = $qq->call('user/get_user_info');

		if($data['ret'] == 0){
			$userInfo['type'] = 'QQ';
			$userInfo['name'] = $data['nickname'];
			$userInfo['nick'] = $data['nickname'];
			$userInfo['head'] = $data['figureurl_2'];
			return $userInfo;
		} else {
			throw_exception("获取腾讯QQ用户信息失败：{$data['msg']}");
		}
	}
	 
	  //注销用户
    public function logout(){
		if(IS_POST)
		{
			session('member',null);
			$msg=array('info'=>'客官,这货终于走了!!!','status'=>1);
			$this->ajaxReturn($msg);
		}
		else
		{
			$this->error("非法访问");
		}
    }
	
	
	//点赞
	 public function like(){
		 if(IS_POST)
		{
			$pid=I('post.pid');
			$like=M('news')->where(array('id'=>$pid))->getField('like');
			$like+=1;
			$data['like']=$like;
			$b=M('news')->where(array('id'=>$pid))->save($data);
			if($b){
				$msg=array('info'=>'哟哟,点击成功了!!!','status'=>1);
			}else{
				$msg=array('info'=>'[捂脸],点击失败了!!!','status'=>0);
			}
			
			$this->ajaxReturn($msg);
		}
		else
		{
			$this->error("非法访问");
		}
		 
	 }
	
	//三行情诗
	 public function love(){
		$love=M('love');
       	$where['status'] = 0;
       	$count =$love->where($where)->count();
		//进行第三方分页类配置
    	$page = array(
    				'total' => $count,//总数
    				'url' => !empty($param['url']) ? $param['url'] : '',//URL配置
    				'max' => !empty($param['max']) ? $param['max'] :100,//每页显示多少条记录
    				'url_model' => 1,//URL模式
    				'ajax' =>  !empty($param['ajax']) ? true : false,//开启ajax分页
    				'out' =>  !empty($param['out']) ? $param['out'] : false,//输出设置
    				'url_suffix' => true,//url后缀
    				'tags' => array('首页','上一页','下一页','尾页'),
    		);
		$p = new \Org\Util\Page($page);
		$list=$love->where($where)->limit($p->pagerows(),$p->maxrows())->order("likes desc")->select();
       	foreach($list as $k=>$v)
        {
          $mid=$v['mid'];
          $penname=M('member')->where(array('uid'=>$mid))->getField('penname');
          $list [$k]['penname']=$penname;
        }
       	//echo $love->_sql();

		
		$this->assign("list", $list); 
		$this->assign('pageNav',$p->get_page()); //输出页码
		 $this->display();
	 }
  	//三行情诗点赞
  	public function love_like(){
      	 if(IS_POST)
		{
			$pid=I('post.id');
           	$love=M('love');
           
           	$ntime = date('Y-m-d',time());
           
           	$ip = get_client_ip(0,true);
			$ip_info = M('love_ip')->where(array('ip'=>$ip,'like_id'=>$pid,'add_time'=>$ntime))->find();
           	if(!empty($ip_info)){
              	if($ip_info['add_time'] == $ntime){
                	$msg=array('info'=>'你今天已经点赞了','status'=>0);$this->ajaxReturn($msg);
                }
                	
            }
             $ip_data['ip'] =$ip;
             $ip_data['num'] =1;
             $ip_data['like_id'] =$pid;
             $ip_data['add_time'] =$ntime;
             M('love_ip')->add($ip_data);
              
			$like=$love->where(array('id'=>$pid))->getField('likes');
			$like+=1;
			$data['likes']=$like;
			$b=$love->where(array('id'=>$pid))->save($data);
			if($b){
				$msg=array('info'=>'哟哟,点击成功了!!!','status'=>1);
			}else{
				$msg=array('info'=>'[捂脸],点击失败了!!!','status'=>0);
			}
			
			$this->ajaxReturn($msg);
		}
		else
		{
			$this->error("非法访问");
		}
    }
	
	//
	public function mei() {
		$this->display();
	}
  
  	//计划任务
  	public function plan(){
    	$news=M('news');
      	$data=$news->field('id,status')->where(array('status'=>0))->order("id")->limit(1)->select();
      	if(!empty($data)){
        	foreach($data as $k=>$v)
			{
              	$info['status'] = 1;
              	$info['published'] = time();
				$news->where(array('id'=>$v['id']))->save($info);
              	echo "执行成功";
			}
        
        }
    }
  
  //商品首页
  	public function shop(){
    	$this->display('shop_index');
    }
  //商品详情页
  public function detail(){
    	$id = $_GET['id'];
    	$news=M('goods');
      	$data=$news->where(array('goods_id'=>$id))->find();
    	$this->assign("list", $data); 
    	$this->display('shop_detail');
    }
	
}